package com.gues.ddc_app.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gues.ddc_app.entity.Feedback;
import com.gues.ddc_app.entity.Shop;
import com.gues.ddc_app.entity.User;
import com.gues.ddc_app.service.IFeedbackService;
import com.gues.ddc_app.service.IShopService;
import com.gues.ddc_app.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.UUID;

@RestController
@CrossOrigin//跨域
public class AllController {
    @Autowired
    private IUserService userService;
    @Autowired
    private IShopService shopService;
    @Autowired
    private IFeedbackService feedbackService;
    @RequestMapping("/imageUpload")
    public Object uploadPortrait(HttpSession session, @RequestParam("file") MultipartFile file){
        String oldName=file.getOriginalFilename();
        String suffix=oldName.substring(oldName.lastIndexOf("."));
        String newName= UUID.randomUUID()+suffix;
        try {
            file.transferTo(new File("D:\\IDEA\\img",newName));
        }catch (IOException e){
            System.out.println("上传异常"+e);
        }
        User user=(User) session.getAttribute("user");
        if(user!=null){
            user.setUserImg(newName);
            userService.updateById(user);
            user=userService.getById(user.getUserId());
            session.setAttribute("user",user);
            return user;
        }
        else {
            Shop shop=(Shop) session.getAttribute("shop");
            shop.setShopImg(newName);
            shopService.updateById(shop);
            shop=shopService.getById(shop.getShopId());
            session.setAttribute("shop",shop);
            return shop;
        }
    }
    @RequestMapping("/upInfo")
    public Object upUserInfo(HttpSession session,Integer id,String name,String address){
        if(session.getAttribute("user")!=null){
            User user=new User();
            user.setUserId(id);
            user.setAddress(address);
            user.setUserName(name);
            boolean b=userService.updateById(user);
            if(b){
                user=userService.getById(id);
                session.setAttribute("user",user);
                return user;
            }
        } else {
            Shop shop=new Shop();
            shop.setShopId(id);
            shop.setShopName(name);
            shop.setCanName(address);
            boolean b=shopService.updateById(shop);
            if (b){
                shop=shopService.getById(id);
                session.setAttribute("shop",shop);
                return shop;
            }
        }
        return null;
    }
    @RequestMapping("/addFeedback")
    public boolean addFeedback(String feedback){
        Feedback feedback1=new Feedback();
        feedback1.setContent(feedback);
        feedback1.setTime(new Date());
        return feedbackService.save(feedback1);
    }
    @RequestMapping("/upPassword")
    public Object upPassword(HttpSession session,Integer id,String password){
        if(session.getAttribute("user")!=null){
            User user=new User();
            user.setUserId(id);
            user.setUserPassword(password);
            boolean b=userService.updateById(user);
            session.setAttribute("user",user);
            return b;
        }
        else {
            Shop shop=new Shop();
            shop.setShopId(id);
            shop.setShopPassword(password);
            boolean b=shopService.updateById(shop);
            session.setAttribute("shop",shop);
            return b;
        }
    }
    @RequestMapping("/allLogout")
    public void  allLogout(HttpSession session){
        if (session.getAttribute("user")!=null){
            session.removeAttribute("user");
        }
        else {
            session.removeAttribute("shop");
        }
    }
    @RequestMapping("/allLogin")
    public Object login(int type, String phone, String password, HttpSession session){
        if(type==1){
            User user=new User();
            user.setUserPhone(phone);
            user.setUserPassword(password);
            QueryWrapper<User> wrapper=new QueryWrapper<>(user);
            user=userService.getOne(wrapper);
            if (user==null){
                return null;
            }
            else {
                session.setAttribute("user",user);
                return user;
            }
        }
        else {
            Shop shop = new Shop();
            shop.setShopPhone(phone);
            shop.setShopPassword(password);
            QueryWrapper<Shop> wrapper=new QueryWrapper<>(shop);
            shop=shopService.getOne(wrapper);
            if (shop==null){
                return null;
            }
            else {
                session.setAttribute("shop",shop);
                return shop;
            }
        }
    }
    @RequestMapping("/allRegister")
    public boolean register(Integer type,String phone,String password,String name,String canName){
        if(type==1){
            User user=new User();
            user.setUserPhone(phone);
            QueryWrapper<User> wrapper=new QueryWrapper<>(user);
            if(userService.list(wrapper).size()!=0){
                return false;
            }
            user.setUserPhone(phone);
            user.setUserName(name);
            user.setUserImg("default.jpg");
            user.setUserPassword(password);
            return userService.save(user);
        }
        else {
            Shop shop = new Shop();
            shop.setShopPhone(phone);
            QueryWrapper<Shop> wrapper=new QueryWrapper<>(shop);
            if(shopService.list(wrapper).size()!=0){
                return false;
            }

            shop.setShopPhone(phone);
            shop.setShopName(name);
            shop.setShopImg("default.jpg");
            shop.setCanName(canName);
            shop.setShopPassword(password);
            return shopService.save(shop);
        }
    }
}
